package com.anolesoft.twelvePlan.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.twelvePlan.entity.MesPlanXiadaJixingDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author zyq
 * @date 2022/6/17
 */
@Mapper
public interface MesPlanXiadaJixingMapper {
    /**
     * 条件查询
     * @param mesPlanXiadaJixingDO
     * @param page
     * @return
     */
    @SelectProvider(type = createSQL.class, method = "queryJXSQL")
    List<MesPlanXiadaJixingDO> queryJX(@Param("mesPlanXiadaJixingDO") MesPlanXiadaJixingDO mesPlanXiadaJixingDO, Page<MesPlanXiadaJixingDO> page);
    public static class createSQL {
        public static String queryJXSQL(@Param("mesPlanXiadaJixingDO") MesPlanXiadaJixingDO mesPlanXiadaJixingDO, Page<MesPlanXiadaJixingDO> page) {
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" a.objuid, a.jixing, a.create_time, a.factory_id, a.nianyue from mes_plan_xiada_jixing a");
                    sql.append(" where a.factory_id = #{mesPlanXiadaJixingDO.factoryId}");
                    if (!StrUtil.isEmptyOrUndefined(mesPlanXiadaJixingDO.getJixing())){
                        sql.append(" and a.jixing = trim(upper(#{mesPlanXiadaJixingDO.jixing}))");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesPlanXiadaJixingDO.getNianyue())) {
                        sql.append(" and a.nianyue = #{mesPlanXiadaJixingDO.nianyue}");
                    }
                    sql.append(" order by a.nianyue, a.jixing");
                    System.out.println("queryJXSQL:" + sql.toString());
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    @Insert("insert into mes_plan_xiada_jixing (jixing, factory_id, create_time, nianyue)" +
            " values (trim(upper(#{mesPlanXiadaJixingDO.jixing})), #{mesPlanXiadaJixingDO.factoryId}, sysdate," +
            " #{mesPlanXiadaJixingDO.nianyue})")
    void insertJX(@Param("mesPlanXiadaJixingDO") MesPlanXiadaJixingDO mesPlanXiadaJixingDO);

    @Select("select count(*) from mes_plan_xiada_jixing a where a.jixing = trim(upper(#{mesPlanXiadaJixingDO.jixing}))" +
            " and a.factory_id = #{mesPlanXiadaJixingDO.factoryId} and a.nianyue = #{mesPlanXiadaJixingDO.nianyue}")
    int repeatJX(@Param("mesPlanXiadaJixingDO") MesPlanXiadaJixingDO mesPlanXiadaJixingDO);
}
